package com.ruoyi.project.system.exam.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.partymember.domain.PartyMember;
import com.ruoyi.project.system.partymember.service.IPartyMemberService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.question.domain.ExamRecord;
import com.ruoyi.project.system.question.service.IExamRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户答题页面Controller
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/exam")
public class ExamUserPageController extends BaseController
{
    private String prefix = "system/exam";

    @Autowired
    private IPartyMemberService partyMemberService;
    
    @Autowired
    private IExamRecordService examRecordService;

    /**
     * 跳转到PK答题页面
     */
    @RequiresPermissions("system:exam:pk:index")
    @GetMapping("/pk/index")
    public String pkIndex(ModelMap mmap)
    {
        // 获取当前登录用户ID
        Long userId = getUserId();
        
        // 根据用户ID获取党员信息
        PartyMember partyMember = partyMemberService.selectPartyMemberByUserId(userId);
        
        // 检查用户是否关联了党员信息
        if (partyMember == null) {
            // 用户未关联党员信息，无法进行PK答题
            mmap.put("errorMsg", "当前登录用户尚未关联党员信息，无法发起PK答题。");
        }
        
        mmap.put("partyMember", partyMember);
        return prefix + "/pk/index";
    }
    
    /**
     * 检查当前用户是否关联了党员信息
     */
    @RequiresPermissions("system:exam:pk:index")
    @RequestMapping("/pk/checkPartyMember")
    @ResponseBody
    public AjaxResult checkPartyMember() {
        // 获取当前登录用户ID
        Long userId = getUserId();
        
        // 根据用户ID获取党员信息
        PartyMember partyMember = partyMemberService.selectPartyMemberByUserId(userId);
        
        if (partyMember == null) {
            return AjaxResult.error("当前登录用户尚未关联党员信息，无法发起PK答题。");
        }
        
        return AjaxResult.success("用户已关联党员信息", partyMember);
    }

    /**
     * 跳转到个人答题页面
     */
    @RequiresPermissions("system:exam:personal:index")
    @GetMapping("/personal/index")
    public String personalIndex(ModelMap mmap)
    {
        // 获取当前登录用户ID
        Long userId = getUserId();
        
        // 根据用户ID获取党员信息
        PartyMember partyMember = partyMemberService.selectPartyMemberByUserId(userId);
        
        // 获取该用户的个人答题记录
        ExamRecord examRecord = new ExamRecord();
        examRecord.setUserId(userId);
        examRecord.setExamType(1); // 个人答题类型
        List<ExamRecord> examRecords = examRecordService.selectExamRecordList(examRecord);
        
        mmap.put("partyMember", partyMember);
        mmap.put("examRecords", examRecords);
        
        return prefix + "/personal/index";
    }
}